/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latticeware.xtalk.stack;

import com.latticeware.xtalk.editor.ScriptEditor;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Type;



/**
 *
 * @author sfisque
 */
class VisualThing
extends Thing
implements ActionListener
{
    protected Component visualEntity;
    
    protected ScriptEditor scriptEditor;
    
    protected Integer id;
    
    
    public VisualThing( Component _component )
    {
        super();

        visualEntity = _component;
        scriptText = "on mouseUp\n\n\nend mouseUp";
    }
    
    
    public VisualThing( Integer _id, Component _component )
    {
        this( _component );
        
        id = _id;
        
    }
    
    
    @Override
    protected Type getType()
    {
        return VisualThing.class;
    }


    public Component getVisualEntity()
    {
        return visualEntity;
    }
    
    
    public void showScriptEditor()
    {
        if( scriptEditor == null )
        {
            createScriptEditor();
        }
        
        scriptEditor.setScript( scriptText );
        scriptEditor.setVisible( true );
    }
    
    
    protected void createScriptEditor()
    {
        scriptEditor = new ScriptEditor();
        scriptEditor.setSaveListener( this );
    }


    @Override
    public void actionPerformed(ActionEvent e)
    {
        if( e.getActionCommand().equals( "Save" ) )
        {
            scriptText = scriptEditor.getScript();
        }
    }
    
    
    protected void setId( Integer _id )
    {
        id = _id;
    }


    public Integer getId()
    {
        return id;
    }

    

    @Override
    public int hashCode()
    {
        return id;
    }


    @Override
    public boolean equals(Object o)
    {
        if( o == null || ! ( o instanceof VisualThing ) )
        {
            return false;
        }
        
        return ((VisualThing) o).id.equals( this.getId() );
    }
}
